// GOAL_Library.cpp : implementation file
//

#include "stdafx.h"
#include "GOAL_Library_Dialog.h"
#include "Library_Functions.h"
#include "GMECOM.h"
#include "ComponentConfig.h"
#include "UdmConfig.h"
#include "UdmUtil.h"

const unsigned int const_boxes[] = {
    GOAL_Library_Dialog::Char,
    GOAL_Library_Dialog::String,
    GOAL_Library_Dialog::Integer,
    GOAL_Library_Dialog::Short,
    GOAL_Library_Dialog::Long,
    GOAL_Library_Dialog::Float,
    GOAL_Library_Dialog::Double
  };

// GOAL_Library dialog

IMPLEMENT_DYNAMIC(GOAL_Library_Dialog, CDialog)

GOAL_Library_Dialog::GOAL_Library_Dialog(GOAL::RootFolder& root,
    Udm::DataNetwork * network,
    CWnd* pParent /*=NULL*/)
	: CDialog(GOAL_Library_Dialog::IDD, pParent), 
    root_ (root), network_ (network)
{
  for (UINT i = 0; i < NUM_BOXES; ++i)
    boxes_results_[i] = GOAL::objectExists (GOAL::Library::const_lookups[i], root_);

}

GOAL_Library_Dialog::~GOAL_Library_Dialog()
{
}

void GOAL_Library_Dialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(GOAL_Library_Dialog, CDialog)
  ON_BN_CLICKED(IDOK, &GOAL_Library_Dialog::OnBnClickedOk)
END_MESSAGE_MAP()

// GOAL_Library message handlers

BOOL GOAL_Library_Dialog::OnInitDialog()
{
  // TODO: Add your control notification handler code here

	CDialog::OnInitDialog();

  for (UINT i = 0; i < NUM_BOXES; ++i)
    CheckDlgButton (const_boxes[i], boxes_results_[i]);

  return TRUE;
}


// GOAL_Library message handlers

void GOAL_Library_Dialog::OnBnClickedOk()
{
  // TODO: Add your control notification handler code here

	CDialog::OnOK();

  std::string message;

  GOAL::Library library;

  for (UINT i = 0; i < NUM_BOXES; ++i)
    {
      if (boxes_results_[i] != IsDlgButtonChecked (const_boxes[i]))
        {
          // if they unchecked the box (boxes_results is the OLD state)
          if (boxes_results_[i])
            {
              Udm::Object remove = GOAL::getObjectByPath (
                GOAL::Library::const_lookups[i],root_);

              remove.DeleteObject ();

              message += GOAL::getChildName (GOAL::Library::const_lookups[i]);
              message += " removed\n";
            }
          // if they checked a box, then insert the library object
          else
            {
              library.createPrimitive (GOAL::Library::const_lookups[i], root_);

              // we do this here to refresh the dialog options. Some classes
              // are created on the fly (like Integer), so we need to do this
              // regardless of the results of primitiveCreate
              boxes_results_[i] = GOAL::objectExists (GOAL::Library::const_lookups[i], root_);

              message += GOAL::getChildName (GOAL::Library::const_lookups[i]);
              message += " created\n";
            }
        }
    }

  AfxMessageBox (message.c_str ());

  
}
